#ifndef CUBE_IMAGE_H
#define CUBE_IMAGE_H

#include <string.h>
#include "color.h"

struct ColorRGB {
    unsigned char r, g, b;
};

float get_k(int lc, float K)
{
    int i;
    float k;
    float total;
    total = 0;
    k = 1;
    for(i = 1; i <= lc-1; ++i)
    {
        total += k;
        k = k*K;
    }
    return total;
}

int get_color(int max, int l, int lc, float K)
{
    float g;
    int count;
    int i;
    float total;
    float k;
    count = lc-l;
    g = 1;
    total = 0;
    for(i = 1; i<=count; ++i) {
        total += g;
        g = g*K;
    }
    k = get_k(lc, K);
    return max-(total*max)/k;
    
}

ColorRGB cols[];
ColorRGB cols2[];

int make_cube_image(SDL_Surface* img, int level)
{
    int y, x;
    //unsigned int r;
    ColorRGB color;
    ColorRGB* pixels;
    
    int dcount;
    int dcount2;
    
    if(level == 1)
        return 0;
    SDL_LockSurface(img);
    printf("Pitch:%d\n", img->pitch);
    pixels = img->pixels;
    dcount = 0;
    dcount2 = 0;
    for(y = 0; y < img->h; ++y)
    {
        for(x = 0; x < img->h; ++x)
        {
            color = pixels[y*img->w+x];
            if(!((color.r<10) && (color.g<10) && 
                (color.b<10))) {
                /*color.g = 50+get_color(205, level, 
                    Levels_Count, frontgK);
                color.b = 100+get_color(155, level, 
                    Levels_Count, frontbK);*/
                color.g=colors1[level-2];
                color.b=colors2[level-2];
                //color = cols[level-2];
                pixels[y*img->w+x] = color;
                if(dcount2 == 0) {
                    dcount2++;
                    printf("Green, Blue %d, %d\n", 
                        color.g, color.b);
                }
                continue;
            }
            //r = get_color(255, level, Levels_Count, cubeK);
            //color.r += r;
            //color.g += r;
            color.r = colors[level-1];
            color.g = colors[level-1];
            //color = cols2[level-2];
            pixels[y*img->w+x] = color;
            if(dcount == 0) {
                dcount++;
                printf("Red %d\n", color.r);
            }
        }
    }
    SDL_UnlockSurface(img);
    return 1;
}

int load_cube_image(int* texture, int level, 
    SDL_Surface* img)
{
    make_cube_image(img, level);
    setup_gl_texture(texture, img, NotAlpha);
    return 1;
}

SDL_Surface* copy_surface(SDL_Surface* from)
{
    SDL_Surface* img;
    img = SDL_CreateRGBSurface(SDL_HWSURFACE, from->w, 
        from->h, from->format->BitsPerPixel,
        from->format->Rmask, from->format->Gmask, 
        from->format->Bmask, from->format->Amask);
    SDL_LockSurface(img);
    SDL_LockSurface(from);
    memcpy(img->pixels, from->pixels, 
        from->w*from->h*from->format->BytesPerPixel);
    SDL_UnlockSurface(img);
    SDL_UnlockSurface(from);
    return img;
}

int target_texture;
int wait_texture;

int make_target_image(SDL_Surface* img)
{
    int w, h;
    ColorRGB color;
    ColorRGB* pixels;
    ColorRGB c;
    color.r = 0;
    color.g = 0;
    color.b = 100;
    SDL_LockSurface(img);
    printf("Pitch %d, w,h %d %d\n", img->pitch
        , img->w, img->h);
    pixels = img->pixels;
    for(h = 0; h < img->h; ++h) {
        for(w = 0; w < img->w; ++w) {
            c = pixels[h*img->w+w];
            //if(c.r >=10)
            //    continue;
            pixels[h*img->w+w] = color; 
        }
    }
    SDL_UnlockSurface(img);
    setup_gl_texture(&target_texture, img, NotAlpha);
    return 1;
}


int make_wait_image(SDL_Surface* img)
{
    int w, h;
    ColorRGB color;
    ColorRGB* pixels;
    ColorRGB c;
    color.r = 200;
    color.g = 0;
    color.b = 0;
    SDL_LockSurface(img);
    pixels = img->pixels;
    for(h = 0; h < img->h; ++h) {
        for(w = 0; w < img->w; ++w) {
            c = pixels[h*img->w+w];
            if(c.r >=10)
                continue;
            pixels[h*img->w+w] = color; 
        }
    }
    SDL_UnlockSurface(img);
    setup_gl_texture(&wait_texture, img, NotAlpha);
    return 1;
}

#endif
